package cn.bonoon.kernel.events;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;

import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.util.StringHelper;

public abstract class AbstractEvent extends BaseEvent{
	protected final HttpServletRequest request;
	private final ApplicationContext applicationContext;

	public AbstractEvent(ApplicationContext applicationContext, HttpServletRequest request, Object source, IOperator operator) {
		super(operator, source);
		this.applicationContext = applicationContext;
		this.request = request;
	}

	
	public String getString(String name) {
		return request.getParameter(name);
	}
	
	public String[] getStringArray(String name) {
		return request.getParameterValues(name);
	}
	
	public boolean is(String name){
		return StringHelper.toboolean(request.getParameter(name));
	}

	public Long getLong(String name) {
		try{
			return Long.parseLong(request.getParameter(name));
		}catch(Exception ex){
			return null;
		}
	}
	
	public Object getAttributeObject(String name){
		return request.getAttribute(name);
	}
	
	public Double getDouble(String name){
		try{
			return Double.parseDouble(request.getParameter(name));
		}catch(Exception ex){
			return null;
		}
	}
	
	public Long getEntityId(){
		Long id = getLong("id");
		if(null == id) return id;
		return decryptId(id);
	}
	
	public Long[] getEntityIds(){
		String[] strs = request.getParameterValues("ids");
		Long[] ids = new Long[strs.length];
		for(int i = 0; i < strs.length; i++){
			ids[i] = decryptId(Long.parseLong(strs[i]));
		}
		return ids;
	}


	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}
}
